package com.ks.config.view;

import com.ks.config.results.BaseResult;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert;

import java.io.Serializable;

@Getter
@Setter
public class CommonReturnVO implements Serializable {

    private boolean success;

    private int code;

    private String msg;

    private Object data;

    public static CommonReturnVO fillByResult(BaseResult baseResult) {
        Assert.notNull(baseResult, "result can't be null");
        CommonReturnVO commonReturnVO = new CommonReturnVO();
        BeanUtils.copyProperties(baseResult, commonReturnVO);
        return commonReturnVO;
    }

    public static CommonReturnVO success() {
        CommonReturnVO commonReturnVO = new CommonReturnVO();
        commonReturnVO.setSuccess(true);
        return commonReturnVO;
    }

    public static CommonReturnVO success(Object data, String msg) {
        CommonReturnVO commonReturnVO = new CommonReturnVO();
        commonReturnVO.setSuccess(true);
        commonReturnVO.setData(data);
        commonReturnVO.setMsg(msg);
        return commonReturnVO;
    }


    public static CommonReturnVO fail() {
        CommonReturnVO commonReturnVO = new CommonReturnVO();
        commonReturnVO.setSuccess(false);
        return commonReturnVO;
    }

    public static CommonReturnVO fail(Object data, String msg) {
        CommonReturnVO commonReturnVO = new CommonReturnVO();
        commonReturnVO.setSuccess(false);
        commonReturnVO.setData(data);
        commonReturnVO.setMsg(msg);
        return commonReturnVO;
    }


    public CommonReturnVO() {

    }

    public CommonReturnVO(boolean success, String msg, Object data) {
        this.success = success;
        this.msg = msg;
        this.data = data;
    }

    public CommonReturnVO(boolean success, int code, String msg, Object data) {
        this.success = success;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
}
